<?php
/**
 * Created by PhpStorm.
 * User: JSQ-SL
 * Date: 2018/10/19
 * Time: 16:16
 */

namespace app\index\table;

use app\index\controller\Login;
use app\index\service\ExcelService;
use think\Db;

class ArchiveBaseTable extends BaseTable
{
    protected static $table = 'ARCHIVE_ASSET_BASE';
    protected static $pk = 'SEQ_NO';

    static public function table(){
        return Db::table(static::$table);
    }

    static public function insert($data){

        $belong = $data['BELONG_NAME'];
        $college = $data['COLLEGE_NAME'];
        $type = $data['TYPE_NAME'];
        $yearmonth = $data['YEARMONTH'];
        $priceInterval = $data['PRICE_INTERVAL'];

        $condition = ['BELONG_NAME'=>$belong,'COLLEGE_NAME'=>$college,
            'TYPE_NAME'=>$type,'YEARMONTH'=>$yearmonth,'PRICE_INTERVAL'=>$priceInterval];

        $old = Db::table(static::$table)->where($condition)->find();
        if(empty($old)){
            $data['SEQ_NO'] = self::getNextId();
            Db::table(static::$table)->insert($data);
        }
        else{
            Db::table(static::$table)->where($condition)->update($data);
        }
    }

    public static function getNextId(){
        $data = Db::table(self::$table)
            ->field('nvl(max(SEQ_NO),0) as NUM')
            ->find();
        if(empty($data) || !isset($data['NUM'])){
            return 1;
        }
        return $data['NUM'] + 1;
    }
}